package com.example.mysqlite;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class StudentAdapter extends BaseAdapter {

    Context context;
    List<StudentBean> students;

    public StudentAdapter(Context context, List<StudentBean> students) {
        this.context = context;
        this.students = students;
    }

    @Override
    public int getCount() {
        return students.size();
    }

    @Override
    public Object getItem(int position) {
        return students.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = View.inflate(context, R.layout.item_layout, null);
            holder = new ViewHolder();
            holder.name = convertView.findViewById(R.id.tv_name);
            holder.number = convertView.findViewById(R.id.tv_number);
            holder.gender = convertView.findViewById(R.id.tv_gender);
            holder.score = convertView.findViewById(R.id.tv_score);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(students.get(position).getName());
        holder.number.setText(students.get(position).getNumber());
        holder.gender.setText(students.get(position).getGender());
        holder.score.setText(students.get(position).getScore());


        return convertView;
    }

    private static class ViewHolder{
        private TextView name;
        private TextView number;
        private TextView gender;
        private TextView score;
    }
}
